import wechatApi from '@/api/wechat'
// import {bindingMobile} from '@/api/login'
import {getToken, setToken, removeToken, setOpenId, removeOpenId, getStatus, setStatus, removeStatus} from '@/utils/auth'
import tools from '@/utils/tools.js'

const user = {
  state: {
    token: getToken(),
    status: getStatus(),
    // userInfo: {
    //   id: '',
    //   username: '',
    //   nickname: '',
    //   mobile: '',
    //   avatar: '',
    //   real_name: ''
    // }
    userInfo: localStorage.getItem('store') ? JSON.parse(localStorage.getItem('store')) : {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo
    }
  },

  actions: {
    // 用户名登录
    LoginByWechatCode({commit}, code) {
      return new Promise((resolve, reject) => {
        // loginByUsername(userInfo).then(response => {
        //   commit('SET_TOKEN', response.token)
        //   setToken(response.token)
        //   commit('SET_ROLES', ['Admin'])

        //   if (response.jwtUser.info && response.jwtUser.info.id) {
        //     resolve({resolve, reject, userId: response.jwtUser.info.id})
        //   } else {
        //     reject(new Error('登录用户没有关联人员信息！'))
        //   }
        // }).catch(error => {
        //   reject(error)
        // })
        wechatApi
          .getWechatInfo(code)
          .then((res) => {
            if (res.data.userinfo.open_id) {
              setOpenId(res.data.userinfo.open_id)
            }
            if (!tools.isNullOrEmpty(res.data.userinfo.token) && res.data.userinfo.status === 'normal') {
              commit('SET_TOKEN', res.data.userinfo.token)
              commit('SET_STATUS', res.data.userinfo.status)
              setToken(res.data.userinfo.token)
              setStatus(res.data.userinfo.status)
              const userInfo = {}
              userInfo.id = res.data.userinfo.user_id
              userInfo.username = res.data.userinfo.username
              userInfo.nickname = res.data.userinfo.nickname
              userInfo.mobile = res.data.userinfo.mobile
              userInfo.avatar = res.data.userinfo.avatar
              userInfo.real_name = res.data.userinfo.real_name
              commit('SET_USERINFO', userInfo)
            }
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
      // .then(({resolve, reject, userId}) => {
      //   getUserInfo(userId).then(response => {
      //     const userInfo = {}
      //     userInfo.id = response.id
      //     userInfo.name = response.nameCn
      //     userInfo.avatar = response.photo
      //     userInfo.institution = response.institutionName
      //     userInfo.code = response.code
      //     userInfo.talentTeamName = response.talentTeamName
      //     userInfo.talentTypeName = response.talentTypeName
      //     userInfo.currentAddress = response.currentAddress
      //     userInfo.email = response.email
      //     userInfo.mobilePhone = response.mobilePhone
      //     userInfo.politicalStatusName = response.politicalStatusName
      //     userInfo.highestDegreeName = response.highestDegreeName
      //     userInfo.highestEducationName = response.highestEducationName
      //     userInfo.workingYears = response.workingYears
      //     userInfo.storageTime = response.storageTime
      //     commit('SET_LOGINUSER', userInfo)
      //     setUserId(response.id)
      //     resolve()
      //   }).catch(error => {
      //     reject(error)
      //   })
      // })
    },

    setLoginData({commit}, res) {
      if (res.data.userinfo && res.data.userinfo.open_id) {
        setOpenId(res.data.userinfo.open_id)
      }
      if (!tools.isNullOrEmpty(res.data.userinfo.token) && res.data.userinfo.status === 'normal') {
        commit('SET_TOKEN', res.data.userinfo.token)
        commit('SET_STATUS', res.data.userinfo.status)
        setToken(res.data.userinfo.token)
        setStatus(res.data.userinfo.status)
        const userInfo = {}
        userInfo.id = res.data.userinfo.user_id
        userInfo.username = res.data.userinfo.username
        userInfo.nickname = res.data.userinfo.nickname
        userInfo.mobile = res.data.userinfo.mobile
        userInfo.avatar = res.data.userinfo.avatar
        userInfo.real_name = res.data.userinfo.real_name
        commit('SET_USERINFO', userInfo)
      }
    },

    // 获取用户信息
    GetUserInfo({commit, state}) {
      return new Promise((resolve, reject) => {
        // getUserInfo(state.loginUser.id).then(response => {
        //   const userInfo = {}
        //   userInfo.id = response.id
        //   userInfo.name = response.nameCn
        //   userInfo.avatar = response.photo
        //   userInfo.institution = response.institutionName
        //   userInfo.code = response.code
        //   userInfo.talentTeamName = response.talentTeamName
        //   userInfo.talentTypeName = response.talentTypeName
        //   userInfo.currentAddress = response.currentAddress
        //   userInfo.email = response.email
        //   userInfo.mobilePhone = response.mobilePhone
        //   userInfo.politicalStatusName = response.politicalStatusName
        //   userInfo.highestDegreeName = response.highestDegreeName
        //   userInfo.highestEducationName = response.highestEducationName
        //   userInfo.workingYears = response.workingYears
        //   userInfo.storageTime = response.storageTime
        //   commit('SET_LOGINUSER', userInfo)
        //   commit('SET_ROLES', ['Admin'])
        //   resolve(response)
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 登出
    LogOut({commit, state}) {
      return new Promise((resolve, reject) => {
        // logout(state.token).then(() => {
        //   commit('SET_TOKEN', '')
        //   commit('SET_LOGINUSER', {})
        //   commit('SET_ROLES', [])
        //   removeToken()
        //   removeUserId()
        //   removeWechatInfo()
        //   resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 前端登出
    FedLogOut({commit}) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_STATUS', '')
        commit('SET_USERINFO', {})
        removeToken()
        removeOpenId()
        removeStatus()
        resolve()
      })
    }
  }
}

export default user
